﻿
@{
    ViewBag.Title = "新闻类型";
    Layout = "~/Areas/Manage/Views/Shared/_SubPage.cshtml";
}

@section Header{
    <link href="~/Content/css/plugins/treeview/bootstrap-treeview.css" rel="stylesheet">}
@section Footer{
    <!-- Bootstrap-Treeview plugin javascript -->
    <script src="~/Content/js/plugins/treeview/bootstrap-treeview.js"></script>
    <script type="text/javascript">
        var TypeList=@MvcHtmlString.Create(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.TypeList));
        var ejApp = angular.module('ejapp', ['angular-icheck']);
        ejApp.controller('mainCtrl', ['$scope', '$http', function ($scope, $http) {
            var idStr = 'Id';
            var addUrl =    '/Manage/News/AddType';
            var updateUrl = '/Manage/News/UpdateType';
            var removeUrl = '/Manage/News/RemoveType';
            $scope.subject = '类型';
            $scope.sIndex = -1;
            $scope.eModel = {};
            $scope.sModel = {};
            $scope.typeList=angular.copy(TypeList);
            $scope.datas = @MvcHtmlString.Create(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.TreeDatas));
            $scope.onSubmit = false;                    //是否提交

            $scope.load = function () {
                $('#treeview').treeview({
                    levels: 99,
                    color: "#428bca",
                    data:  $scope.datas,
                    onNodeSelected: function (event, node) {
                        $scope.$apply(function () {
                            $scope.sIndex = node.Id
                            $scope.sModel = angular.copy(node);
                        });
                    }
                });
            };
            $scope.doAdd = function () {                    //添加             
                $scope.onSubmit = false;
                $scope.eModel = {
                    'Id': 0,
                    'DispayIndex':1,
                    'ParentId':0,
                    'IsEnabled':true                    
                }
                $scope.sIndex=-1;
                $('#modal-edit').modal();
            };
            $scope.doUpdate = function () {            //编辑
                if($scope.sIndex == -1)
                {
                    EJ.showError("请点击要操作的类型");
                    return;
                }
                $scope.eModel=angular.copy($scope.sModel);
                $('#modal-edit').modal();
            }
            $scope.doRemove = function () {                //删除指定对象
                if($scope.sIndex==-1)
                {
                    EJ.showError("请点击要操作的类型");
                    return;
                }
                EJ.confimWarn('确定要删除指定的' + $scope.subject + '吗?', function () {
                    $http.post(removeUrl, { "id": $scope.sIndex })
                    .success(function (rsp) {
                        if (rsp.Success) {
                            EJ.showSuccess("删除成功");
                            //$scope.datas.remove($scope.eModel);
                            setTimeout(function(){
                                window.location.href="/Manage/News/TypeIndex";
                            },1000);
                        } else {
                            EJ.showError(rsp.Message);
                        }
                    })
                    .error(errorFn);
                });
            }
            $scope.save = function (valid) {
                $scope.onSubmit = true;
                if (!valid) return;
                showLoading();
                var isAdd = $scope.sIndex == -1;
                var url = isAdd ? addUrl : updateUrl;
                $http.post(url, $scope.eModel)
                    .success(function (rsp) {
                        if (rsp.Success) {
                            EJ.showSuccess("保存成功!");
                            $('#modal-edit').modal('hide');
                            setTimeout(function(){
                                window.location.href="/Manage/News/TypeIndex";
                            },1000);
                        } else {
                            EJ.showError(rsp.Message);
                        }
                        hideLoading();
                    })
                    .error(errorFn)
            }
            $scope.$watch('pSize', function () { $scope.pIndex = 1; $scope.load(); });                              //会导致初始化加载

        }]).filter('convertDate', function () {
            var convertTime = function (date) {
                return EjConvertDateFormat(date, "yyyy年MM月dd日 HH:mm:ss");
            }
            return convertTime;
        })
    </script>
}
<div class="wrapper wrapper-content animated fadeInUp">
    <div class="row  animated slideInRight">
        <div class="col-sm-12">
            <div class="ibox">
                <div class="ibox-title">
                    <h5>新闻类型</h5>
                    <div class="ibox-tools">
                        <button ng-click='doAdd()' class="btn btn-primary btn-xs" type="button"><i class='fa fa-plus'></i>&nbsp;新增</button>
                        <button ng-click='doUpdate()' class="btn btn-info btn-xs" type="button"><i class='fa fa-edit'></i>&nbsp;修改</button>
                        <button ng-click='doRemove()' class="btn btn-danger btn-xs" type="button"><i class='fa fa-trash-o'></i>&nbsp;删除</button>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="col-sm-8"><div id="treeview" class="test"></div></div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="modal-edit" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="modal-stackable-label" aria-hidden="true" class="modal fade">
    <div class="modal-dialog">
        <form name="eForm" ng-submit="save(eForm.$valid)" class="form-horizontal form-bordered form-row-stripped" role="form" novalidate>
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" data-dismiss="modal" aria-hidden="true" class="close">&times;</button>
                    <h4 id="modal-stackable-label" class="modal-title">{{subject}}-编辑</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-sm-3">
                            <label class="control-label">上级Id:<span class="require">*</span></label>
                            <select name="ParentId" class="form-control" ng-model="eModel.ParentId" >
                                <option value="">==请选择==</option>
                                <option value="{{item.Id}}" ng-repeat="item in typeList" ng-selected="eModel.ParentId==item.Id">{{item.Name}}</option>
                            </select>
                        </div>
                        <div class="col-sm-3" ng-class="{'state-error':eForm.DispayIndex.$invalid&&onSubmit}">
                            <label class="control-label">排序:<span class="require">*</span></label>
                            <input type="text" name="DispayIndex" ng-model="eModel.DispayIndex" ng-required="true" class="form-control" />
                            <em ng-show="eForm.DispayIndex.$invalid&&onSubmit" class="invalid">必填字段!</em>
                        </div>                        
                        <div class="col-sm-3">
                            <p class="m-t">
                                <input id="cbIsEnabled" type="checkbox" icheck value="true" ng-model="eModel.IsEnabled" />
                                <label for='cbIsEnabled'>是否启用</label>
                            </p>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12" ng-class="{'state-error':eForm.Name.$invalid&&onSubmit}">
                            <label class="control-label">名称:<span class="require">*</span></label>
                            <input type="text" name="Name" ng-model="eModel.Name" ng-required="true" class="form-control" />
                            <em ng-show="eForm.Name.$invalid&&onSubmit" class="invalid">必填字段!</em>
                        </div>                        
                    </div>                   
                </div>
                <div class="modal-footer">
                    <button type="submit" id="btnSave" class="btn btn-primary">保存</button>
                    <button type="button" data-dismiss="modal" class="btn btn-default">关闭</button>
                </div>
            </div>
        </form>
    </div>
</div>